﻿namespace Sai.Core.Sandcastle
{
	using System.IO;

	public static class TopicIdReader
	{
		public static Identifier LoadFrom(string filePath)
		{
			using (var topicFileReader = new StreamReader(filePath))
			{
				return TopicIdReader.LoadFrom(topicFileReader);
			}
		}

		public static Identifier LoadFrom(TextReader reader)
		{
			// extremely simple - not treating project file as xml or build file
			// only interested in search and replace for some files
			const string TopicTag = "<topic";
			string topicId = string.Empty;

			string line;
			while ((line = reader.ReadLine()) != null)
			{
				if (!string.IsNullOrEmpty(line) 
					&& line.Trim().StartsWith(TopicTag))
				{
					topicId = line.ExtractQuotedText();
				}
			}

			return new Identifier(topicId);
		}
	}
}
